package com.aube.plugin.origin;

import com.huyn.baseframework.model.ChannelDetail;
import com.huyn.baseframework.model.Character;
import com.huyn.baseframework.model.VideoDefinition;
import com.huyn.baseframework.model.VideoWithDefinitionModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by huyaonan on 16/6/2.
 */
public class DataConvertUtil {

    /**
     * //解析首页顶图数据
     {
         "content": {
             "showid": "2",
             "videoid": "1",
             "videoTitle": "中国好声音  第三期",
             "videoDesc": "121323",
             "playTimestamp": "2016-03-18 12:00:00",
             "duration": 0,
             "picinfo": {
                 "picurl": "http://192.168.1.103/static/images/201511/common/s_44e14403_1514c3371b2__7fff.png",
                 "width": "1180",
                 "height": "561"
                 }
         },
         "title": "中国好声音  第三期",
         "headPic": "http://192.168.1.103/static/images/201511/common/s_44e14403_1514c3371b2__7fff.png",
         "type": "video"
     },
     {
         "content": {
            "url": "http://www.baidu.com/"
         },
         "title": "景深位图片测试",
         "headPic": {
                "picurl": "http://192.168.1.103/static/images/201511/common/s_44e14403_1514c3371b2__7fff.png",
                }
         "type": "h5"
     }
     */
    public static List<SimpleIndexHeader> parseIndexHeader(List obj) {
        if(obj == null)
            return null;

        List<SimpleIndexHeader> result = new ArrayList<>();
        try {
            SimpleIndexHeader header;
            for(int i=0; i<obj.size(); i++) {
                header = new SimpleIndexHeader();
                LinkedHashMap<String, Object> lhm = (LinkedHashMap<String, Object>) obj.get(i);
                String type = (String) lhm.get("type");
                header.type = type;
                if ("video".equalsIgnoreCase(type)) {
//                    header.title = (String) lhm.get("title");
                    header.picurl = (String) lhm.get("newHeadPic");
                    header.title = (String) lhm.get("newTitle");
                    header.desc = (String) lhm.get("newDesc");

                    header.channel = new ChannelDetail();
                    LinkedHashMap<String, Object> content = (LinkedHashMap<String, Object>) lhm.get("content");
                    header.channel.showid = (String) content.get("showid");
                    header.channel.videoid = (String) content.get("videoid");
                    header.channel.videoTitle = (String) content.get("videoTitle");
                    header.channel.videoDesc = (String) content.get("videoDesc");
                    header.channel.playTimestamp = (String) content.get("playTimestamp");
                    header.channel.duration = (int) ((double) content.get("duration"));
                    header.channel.videono = (String) content.get("videono");
                    header.channel.cameraType = (String) content.get("cameraType");
                    header.channel.updownAngle = (String) content.get("updownAngle");
                    header.channel.leftRightAngle = (String) content.get("leftRightAngle");

                    if (content.containsKey("videoPlay")) {
                        VideoWithDefinitionModel videoPlay = new VideoWithDefinitionModel();
                        LinkedHashMap<String, Object> set = (LinkedHashMap<String, Object>) content.get("videoPlay");
                        if (set.containsKey("defaultdefinition")) {
                            videoPlay.defaultdefinition = (int) ((double) set.get("defaultdefinition"));
                        }
                        if (set.containsKey("definitionDescription")) {
                            List<Object> videos = (List<Object>) set.get("definitionDescription");
                            if (videos != null) {
                                List<String> definitions = new ArrayList<>();
                                for (Object o : videos)
                                    definitions.add((String) o);
                                videoPlay.definitionDescription = definitions;
                            }
                        }
                        if (set.containsKey("definitions")) {
                            List<Object> list = (List<Object>) set.get("definitions");
                            if (list != null) {
                                List<VideoDefinition> definitionList = new ArrayList<>();
                                for (Object o : list) {
                                    LinkedHashMap<String, Object> item = (LinkedHashMap<String, Object>) o;
                                    if (item != null) {
                                        VideoDefinition definition = new VideoDefinition();
                                        definition.definition = (int) ((double) item.get("definition"));
                                        definition.desp = (String) item.get("desp");
                                        definition.playurl = (String) item.get("playurl");
                                        definitionList.add(definition);
                                    }
                                }
                                videoPlay.definitions = definitionList;
                            }
                        }
                        header.channel.videoPlay = videoPlay;
                    }
                } else if ("h5".equalsIgnoreCase(type)) {
                    header.title = (String) lhm.get("title");

                    LinkedHashMap<String, Object> content = (LinkedHashMap<String, Object>) lhm.get("content");
                    header.url = (String) content.get("url");

                    LinkedHashMap<String, Object> headPic = (LinkedHashMap<String, Object>) lhm.get("headPic");
                    header.picurl = (String) headPic.get("picurl");
                }
                result.add(header);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public static SimpleIndexHeader parsePreviousHeader(List obj) {
        if(obj == null)
            return null;

        try {
            SimpleIndexHeader header;
            for(int i=0; i<obj.size(); i++) {
                header = new SimpleIndexHeader();
                LinkedHashMap<String, Object> lhm = (LinkedHashMap<String, Object>) obj.get(i);
                header.picurl = (String) lhm.get("newHeadPic");
                header.title = (String) lhm.get("newTitle");
                return header;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * {
         "praiseCount": 0,
         "relatedId": "2465",
         "headPic": {
             "picurl": "http://static01.aube-tv.com/upload/images/actor/201605/s151cded0_154ece7b6d9__7fcf.jpg",
             "width": "1440",
             "height": "900"
         },
         "actorName": "海贼王2"
     }
     * @param obj
     */
    public static SimpleCharacter parseActorHeader(List obj) {
        if(obj == null)
            return null;

        for(int i=0; i<obj.size(); i++) {
            SimpleCharacter character = new SimpleCharacter();
            LinkedHashMap<String, Object> lhm = (LinkedHashMap<String, Object>) obj.get(i);
            character.praiseCount = (int) ((double) lhm.get("praiseCount"));
            character.actorName = (String) lhm.get("actorName");
            character.relatedId = (String) lhm.get("relatedId");
            character.praised = (String) lhm.get("praised");

            LinkedHashMap<String, Object> headPic = (LinkedHashMap<String, Object>) lhm.get("headPic");
            if (headPic != null)
                character.picurl = (String) headPic.get("picurl");
            return character;
        }
        return null;
    }

    public static class SimpleCharacter implements Serializable {
        public int praiseCount;
        public String actorName;
        public String relatedId;
        public String picurl;
        public String praised;

        public boolean isPraised() {
            return "Y".equalsIgnoreCase(praised);
        }
    }

    public static class SimpleIndexHeader implements Serializable {
        public ChannelDetail channel;
        public String title;
        public String desc;
        public String picurl;
        public String type;
        public String url;

        public boolean isH5() {
            return "h5".equalsIgnoreCase(type);
        }

    }

    public static SimpleCharacter toSimpleCharacter(Character character) {
        SimpleCharacter sItem = new SimpleCharacter();
        sItem.praiseCount = character.praiseCount;
        sItem.actorName = character.actorName;
        sItem.relatedId = character.relatedId;
        sItem.picurl = character.actorPic;
        sItem.praised = character.praised;
        return sItem;
    }

}
